package algos;

import java.util.List;
import java.util.Map;

import ue.UE;
import eNodeB.ENodeB;
import eNodeB.RessourceBlock;

public class RoundRobin extends AAlgos {

	public RoundRobin(ENodeB eNodeB) {
		super(eNodeB);
	}

	@Override
	public void run() {
		eNodeB.freeMapRessourceBlocks();
		eNodeB.freeBusyRessourceBlocks();
		List<UE> users = eNodeB.getActualUE();
		if (!users.isEmpty()) {

			try {
				eNodeB.getSem().acquire();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			while (eNodeB.getPoolRessourceBlocksSize() != 0) {
				for (UE ue : users) {
					RessourceBlock rb = eNodeB.getFirstFreeRessourceBlock();
					if (rb != null) {
						schedule(ue, rb);
					}

				}

			}

			for (Map.Entry<UE, List<RessourceBlock>> entry : eNodeB
					.getRessourceBlockMapping().entrySet()) {
				eNodeB.sendPacketPDCCH(entry.getKey(), entry.getValue());
			}

			eNodeB.getSem().release();
		}
	}
}
